<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 作业列表 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-select
                    style="width: 200px"
                    v-model="jobSubmitCheckVo.jobSubmit.status"
                    placeholder="提交作业的状态"
                    class="handle-select mr10"
                >
                    <el-option key="a" label="未审批" value="a"></el-option>
                    <el-option key="b" label="已审批" value="b"></el-option>
                    <el-option key="c" label="未提交" value="c"></el-option> </el-select
                ><el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>&nbsp;
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column prop="student.name" label="学生姓名"></el-table-column>
                <el-table-column label="作业状态" align="center">
                    <template slot-scope="scope">
                        <el-tag>
                            <span v-if="scope.row.status == 'a'">未审批</span>
                            <span v-if="scope.row.status == 'b'">已审批</span>
                            <span v-if="scope.row.status == 'c'">未提交</span>
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="评级" align="center">
                    <template slot-scope="scope">
                        <el-tag>
                            <span v-if="scope.row.level == null">未审批或未提交</span>
                            <span v-if="scope.row.level != null">{{ scope.row.level }}</span>
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="评价" align="center">
                    <template slot-scope="scope">
                        <span v-if="scope.row.evaluation == null">未审批或未提交</span>
                        <span v-if="scope.row.evaluation != null">{{ scope.row.evaluation }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleUpdate(scope.$index, scope.row, scope.row.id)"
                            ><span v-if="scope.row.evaluation == null">批改作业</span>
                            <span v-if="scope.row.evaluation != null">修改批改</span></el-button
                        >
                    </template>
                </el-table-column>
            </el-table>

            <div class="pagination">
                <el-pagination
                    background
                    layout="prev, pager, next"
                    :total="totalPages"
                    :current-page="currentPage"
                    @current-change="handlePageChange($event)"
                >
                </el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="作业详情" :visible.sync="updateVisible" width="30%" :close-on-click-modal="false" :before-close="handleClose">
            <el-form ref="form" :model="form" label-width="70px">
                <span style="margin-right: 15px"><b>作业内容</b></span
                ><span style="margin-right: 100px"> {{ form.job.content }}</span
                ><br />
                <el-form-item label="作业图片">
                    <el-image
                        v-for="(item, index) in jobUrls"
                        :key="index"
                        :src="item.url"
                        :preview-src-list="getImgList(index)"
                        style="width: 100px; height: 100px"
                    ></el-image>
                </el-form-item>
                <span style="margin-right: 15px"><b>学生姓名</b></span
                ><span style="margin-right: 100px"> {{ form.jobSubmit.student.name }}</span
                ><br />

                <el-form-item label="提交作业">
                    <el-image
                        v-for="(item, index) in urls"
                        :key="index"
                        :src="item.url"
                        :preview-src-list="getImgList(index)"
                        style="width: 100px; height: 100px"
                    ></el-image>
                </el-form-item>
            </el-form>

            <el-dialog width="30%" title="批改" :visible.sync="innerVisible" append-to-body>
                <el-form ref="form" :model="jobSubmit" label-width="70px">
                    <el-form-item label="评级">
                        <el-select v-model="jobSubmit.level" placeholder="请选择">
                            <el-option key="A" label="A" value="A"></el-option>
                            <el-option key="B" label="B" value="B"></el-option>
                            <el-option key="C" label="C" value="C"></el-option>
                            <el-option key="D" label="D" value="D"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="评价">
                        <el-input type="textarea" rows="5" v-model="jobSubmit.evaluation"> </el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="innerVisible = false">取 消</el-button>
                    <el-button type="primary" @click="update">批 改</el-button>
                </span>
            </el-dialog>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="inner()">批 改</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { fetchData } from '../../api/index';
export default {
    name: 'basetable',
    data() {
        return {
            tableData: [],
            multipleSelection: [],
            delList: [],
            updateVisible: false,
            innerVisible: false,
            form: {
                job: {
                    content: '',
                    jobPhotos: []
                },
                jobSubmit: {
                    student: {
                        name: ''
                    },
                    jobSubmitPhotos: []
                }
            },
            totalPages: 0,
            currentPage: 0,
            jobSubmitCheckVo: {
                jobSubmit: {
                    jobId: '',
                    status: ''
                },
                page: 1
            },
            urls: [],
            jobUrls: [],
            jobSubmit: {
                id: '',
                level: '',
                evaluation: '',
                status: ''
            }
        };
    },
    created() {
        this.jobSubmitCheckVo.jobSubmit.jobId = this.$route.query.id;
        this.getList();
        console.log(this.jobSubmitCheckVo);
    },
    methods: {
        //初始化列表
        getList() {
            this.$axios.post('/job/admin/job/getJobSubmit', this.jobSubmitCheckVo).then((r) => {
                console.log(r);
                if (r.data.code == 200) {
                    this.tableData = r.data.data.data;
                    this.totalPages = r.data.data.totalPages * 10;
                    this.currentPage = r.data.data.currPage;
                    console.log(r.data.data.totalPages);
                } else if (r.data.code == 403) {
                }
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.jobSubmitCheckVo.page = this.currentPage;
            console.log(this.currentPage);
            this.getList();
        },
        // 批改作业
        handleUpdate(index, row, id) {
            if (row.status == 'c') {
                this.$message.error('未提交作业，不可批改');
                return;
            }
            this.$axios.get('/job/admin/job/detailJobSubMit/' + id).then((r) => {
                console.log(r);
                if (r.data.code == 200) {
                    this.form = r.data.data;
                    console.log(this.form);
                    this.urls = r.data.data.jobSubmit.jobSubmitPhotos;
                    console.log(this.urls);
                    this.jobUrls = r.data.data.job.jobPhotos;
                    this.jobSubmit.level = r.data.data.jobSubmit.level;
                    this.jobSubmit.evaluation = r.data.data.jobSubmit.evaluation;
                    this.jobSubmit.status = r.data.data.jobSubmit.status;
                } else if (r.data.code == 403) {
                }
            });
            this.jobSubmit.id = id;
            this.idx = index;
            this.form = row;
            this.updateVisible = true;
        },
        // 分页导航
        handlePageChange(currentPage) {
            this.jobSubmitCheckVo.page = currentPage;
            this.getList();
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then((_) => {
                    done();
                })
                .catch((_) => {});
        },
        //批改
        inner(id) {
            this.innerVisible = true;
        },
        update() {
            this.$axios.post('/job/admin/job/correct', this.jobSubmit).then((r) => {
                console.log(r);
                if (r.data.code == 200) {
                    this.updateVisible = false;
                    this.innerVisible = false;
                    this.$message.success('成功批改作业！');
                    location.reload();
                } else {
                    this.$message.error('作业批改失败了，请过会再试！');
                }
            });
        },
        //放大图片
        getImgList(index) {
            let arr = [];
            let i = 0;
            for (i; i < this.urls.length; i++) {
                arr.push(this.urls[i + index].url);
                if (i + index >= this.urls.length - 1) {
                    index = 0 - (i + 1);
                }
            }
            return arr;
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
